
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:math';
// import 'alert_msg_util.dart';
// import '../../tool/Toast.dart';
import 'package:fluttertoast/fluttertoast.dart';
 
class MapUtil {
 
  /// 高德地图
  static Future<bool> gotoAMap(longitude, latitude,gasaddress) async {

     double x = double.parse(latitude), y = double.parse(longitude);
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * pi);
                    double theta = atan2(y, x) + 0.000003 * cos(x * pi);
      double bd_lat = z * cos(theta) + 0.0065;
      double bd_lon = z * sin(theta) + 0.006;
      print(bd_lon);
      print(bd_lat);

    // var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat='+bd_lat.toString()+'&lon='+bd_lon.toString()+'&dev=0&style=2';
 
 var url = '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';
    bool canLaunchUrl = await canLaunch(url);
 
    if (!canLaunchUrl) {
      Fluttertoast.showToast(msg:'未检测到高德地图~');
      return false;
    }
 
    await launch(url);
 
    return true;
  }
 
  /// 腾讯地图
  static Future<bool> gotoTencentMap(longitude, latitude,gasaddress) async {
     double x = double.parse(latitude), y = double.parse(longitude);
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * pi);
                    double theta = atan2(y, x) + 0.000003 * cos(x * pi);
      double bd_lat = z * cos(theta) + 0.0065;
      double bd_lon = z * sin(theta) + 0.006;
      print(bd_lon);
      print(bd_lat);
    var url = 'qqmap://map/routeplan?type=drive&tocoord=$bd_lat,$bd_lon&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';
    bool canLaunchUrl = await canLaunch(url);
 
    if (!canLaunchUrl) {
      // ToastUtil.show('未检测到腾讯地图~');
      Fluttertoast.showToast(msg:'未检测到腾讯地图~');
      return false;
    }
 
    await launch(url);
 
    return canLaunchUrl;
  }
 
  /// 百度地图
  static Future<bool> gotoBaiduMap(longitude, latitude,gasaddress) async {
    
     double x = double.parse(latitude), y = double.parse(longitude);
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * pi);
                    double theta = atan2(y, x) + 0.000003 * cos(x * pi);
      double bd_lat = z * cos(theta) + 0.0065;
      double bd_lon = z * sin(theta) + 0.006;

var url = 'baidumap://map/direction?destination='+bd_lat.toString()+','+bd_lon.toString()+'&coord_type=bd09ll&mode=driving';
    // var url = 'baidumap://map/direction?destination=$latitude,$longitude&coord_type=bd09ll&mode=driving';
  // var url = "baidumap://map/direction?destination=$gasaddress&mode=driving&coord_type=gcj02";
    bool canLaunchUrl = await canLaunch(url);
 
    if (!canLaunchUrl) {
      // ToastUtil.show('未检测到百度地图~');
       Fluttertoast.showToast(msg:'未检测到百度地图~');
      return false;
    }
 
    await launch(url);
 
    return canLaunchUrl;
  }
 
  /// 苹果地图
  static Future<bool> gotoAppleMap(String geo) async {
    var url = 'https://maps.apple.com/?sll=$geo&t=s';
 
    bool canLaunchUrl = await canLaunch(url);
 
    if (!canLaunchUrl) {
      // ToastUtil.show('打开失败~');
      Fluttertoast.showToast(msg:'打开失败~');
      return false;
    }
 
    await launch(url);
  }
}